package com.example.demo.mapper;

import com.example.demo.entity.Book;
import com.example.demo.entity.Borrow;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookMapper {
    @Select("""
    select * from borrow left join book on borrow.bid = book.id left join student
    on borrow.sid = student.id
    """)
    @Results(value = {
            @Result(column = "id", property = "id"),
            @Result(column = "sid", property = "sid"),
            @Result(column = "bid", property = "bid"),
            @Result(column = "time", property = "time"),
            @Result(column = "title", property = "bookName"),
            @Result(column = "name", property = "studentName")
    })
    List<Borrow> getBorrowList();

    @Insert("insert into borrow(sid, bid, time) values (#{sid}, #{bid}, 'NOW')")
    Boolean addBorrow(@Param("sid") int sid, @Param("bid") int bid);

    @Delete("delete from borrow where id = #{id}")
    Boolean deleteBorrow(int id);

    @Select("select * from book")
    List<Book> getBookList();

    @Delete("delete from book where id = #{id}")
    Boolean deleteBook(int id);

    @Insert("insert into book(title, description, price) values (#{title}, #{description}, #{price})")
    Boolean addBook(@Param("title") String title, @Param("description") String description, @Param("price") double price);
}
